﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPFEdit.Document.Factory;
using WPFEdit.Document;
using System.IO;

namespace SampleApp
{
	class StringHolder : IEditable
	{
		private bool m_dirty;
		private string m_data;

		public string Data
		{
			get { return m_data; }
			set { m_data = value; }
		}
		

		public bool Dirty
		{
			get
			{
				return m_dirty;
			}
			set
			{
				if (m_dirty != value)
				{
					m_dirty = value;
					if (DirtyChanged != null) DirtyChanged(this, new ValueChangedEventArgs<bool>(!m_dirty, m_dirty));
				}
			}
		}

		public bool Loaded
		{
			get;
			set;
		}

		public event ValueChangedEventHandler<bool> DirtyChanged;
	}

	[DocumentReader( new string [] { ".txt" })]
	class SampleReader : IDocumentReader
	{
		public Document ReadDocument(System.IO.Stream input, string extension)
		{
			Document doc = new Document();

			StreamReader str = new StreamReader(input);
			var data = new StringHolder();
			data.Data = str.ReadToEnd();
			doc.DocumentData = data;

			return doc;
		}
	}

	[DocumentWriter( typeof(StringHolder), false)]
	class SampleWriter : IDocumentWriter
	{

		public void WriteDocument(Stream output, Document doc)
		{
			StreamWriter writer = new StreamWriter(output);
			var data = (StringHolder)doc.DocumentData;
			writer.Write(data.Data);
			writer.Flush();
		}
	}
}
